//
//  TradeDetailVC.swift
//  kxf
//
//  Created by LNS2 on 2024/4/24.
//

import Foundation

class TradeDetailVC: WHBaseViewVC {
    
    var tradeDict = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        initUI()
    }
    lazy var whiteView: UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        
        return vi
    }()
    lazy var iconImgView: UIImageView = {
        let img = UIImageView()
        if tradeDict["payType"]as? Int ?? 0 == 2 {//微信
            img.setImgLocal(imgName: "pay_type_wechat_icon")
        }else if tradeDict["payType"]as? Int ?? 0 == 3 {//支付宝
            img.setImgLocal(imgName: "pay_type_alipay_icon")
        }else if tradeDict["channel"]as? String ?? "" == "首信易钱包"{
            img.setImgLocal(imgName: "pay_type_sxy_icon")
        }else{
            img.setImgLocal(imgName: "pay_type_unipay_icon")
        }
        return img
    }()
    lazy var moneyLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.font = .systemFont(ofSize: 24, weight: .medium)
        lab.text = "¥ \(WHUtils.convertStringToString("\(tradeDict["amount"]as? Double ?? 0.00)") ?? "0.00")"
        
        return lab
    }()
    lazy var timeVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(160), width: 0, height: 0))
        vm.titleLabel.text = "交易时间"
        vm.rightLabel.text = "\(Date().timestampToDate(timestamp: TimeInterval((tradeDict["createTime"]as? Int ?? 0)/1000)))"
        return vm
    }()
    lazy var handFeeVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.timeVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "交易手续费"
        vm.rightLabel.text = "¥\(WHUtils.convertStringToString("\(tradeDict["fee"]as? Double ?? 0.00)") ?? "0.00")"
        return vm
    }()
    lazy var terminalVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.handFeeVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "终端号"
        vm.rightLabel.text = "\(tradeDict["posNumber"]as? String ?? "-")"
        return vm
    }()
    lazy var payTypeVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.terminalVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "支付类型"
        vm.rightLabel.text = "\(tradeDict["bankName"]as? String ?? "-")"
        return vm
    }()
    lazy var orderNoVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.payTypeVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "订单号"
        vm.rightLabel.text = "\(tradeDict["showUserOrderId"]as? String ?? "-")"
        return vm
    }()
    lazy var payStatusVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.orderNoVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "支付状态"
        vm.rightLabel.text = "\(tradeDict["fee"]as? Double ?? 0)"
        if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 0 {
            vm.rightLabel.text = "交易成功"
        }else if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 1{
            vm.rightLabel.text = "交易失败"
        }else if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 2{
            vm.rightLabel.text = "未支付"
        }else if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 3{
            vm.rightLabel.text = "交易中"
        }else if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 4{
            vm.rightLabel.text = "退款中"
        }else if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 5{
            vm.rightLabel.text = "退款成功"
        }else if tradeDict["userPayOrderStatus"]as? Int ?? -1 == 6{
            vm.rightLabel.text = "退款失败"
        }
        return vm
    }()
    lazy var operaStatusVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.payStatusVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "结算状态"
        vm.rightLabel.text = "\(tradeDict["fee"]as? Double ?? 0)"
        if tradeDict["payUserOrderOperaStatus"]as? Int ?? -1 == 0 {
            vm.rightLabel.text = "结算成功"
        }else if tradeDict["payUserOrderOperaStatus"]as? Int ?? -1 == 1{
            vm.rightLabel.text = "结算失败"
        }else if tradeDict["payUserOrderOperaStatus"]as? Int ?? -1 == 2{
            vm.rightLabel.text = "未结算"
        }else if tradeDict["payUserOrderOperaStatus"]as? Int ?? -1 == 3{
            vm.rightLabel.text = "结算中"
        }else if tradeDict["payUserOrderOperaStatus"]as? Int ?? -1 == 8{
            vm.rightLabel.text = "延迟结算中"
        }
        return vm
    }()
    lazy var remarkVm: TradeDetailItemVM = {
        let vm = TradeDetailItemVM.init(frame: CGRect.init(x: 0, y: self.operaStatusVm.frame.maxY, width: 0, height: 0))
        vm.titleLabel.text = "付款说明"
        vm.rightLabel.text = "\(tradeDict["paymentInstructions"]as? String ?? "-")"
        if tradeDict["paymentInstructions"]as? String ?? "" == ""{
            vm.isHidden = true
        }
        return vm
    }()
}

extension TradeDetailVC{
    func initUI() {
        initNavi(titleStr: "交易详情")
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        view.addSubview(whiteView)
        whiteView.addSubview(iconImgView)
        whiteView.addSubview(moneyLabel)
        view.addSubview(timeVm)
        view.addSubview(handFeeVm)
        view.addSubview(terminalVm)
        view.addSubview(payTypeVm)
        view.addSubview(orderNoVm)
        view.addSubview(payStatusVm)
        view.addSubview(operaStatusVm)
        view.addSubview(remarkVm)
        
        setConstrait()
    }
    func setConstrait(){
        whiteView.snp.makeConstraints { make in
            make.left.width.equalToSuperview()
            make.top.equalTo(getNavigationBarHeight()+kFitWidth(10))
            make.height.equalTo(kFitWidth(150))
        }
        iconImgView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(kFitWidth(30))
            make.width.height.equalTo(kFitWidth(56))
        }
        moneyLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(iconImgView.snp.bottom).offset(kFitWidth(20))
        }
    }
}
